<template>
  <div id="divbox">
    <!-- 登录表单 -->
    <div class="login-form">
      <div id="logo">
        <span class="login-title">
          欢迎登录<span style="color: rgb(137, 204, 255)">健身管理系统</span>
        </span>
      </div>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <!-- 用户名 -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            @click="handleLogin"
            style="width: 100%; margin-bottom: 10px"
          >
            登录
          </el-button>
          <!-- 注册提示 -->
          <span class="register" @click="goToRegister">没有账号？点击注册</span>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { loginService } from "../../utils/user";

// 表单数据
const form = reactive({
  email: "",
  password: "",
});

// 表单校验规则
const rules = {
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱地址",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码！", trigger: "blur" },
    { min: 6, message: "密码长度至少 6 位", trigger: "blur" },
  ],
};

// 表单引用
const formRef = ref();

// 路由实例
const router = useRouter();

// 登录提交逻辑
const onSubmit = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      // 模拟登录验证
      if (form.email === "admin@qq.com" && form.password === "123456") {
        ElMessage.success("登录成功！");
        router.push("/guanli"); // 跳转到主页
      } else {
        ElMessage.error("用户名或密码错误！");
      }
    } else {
      ElMessage.error("请完善表单信息！");
    }
  });
};

// 跳转到注册页面
const goToRegister = () => {
  router.push("/register");
};
//登录
const handleLogin = async function () {
  if (!form.email || !form.password) {
    ElMessage.error("请输入用户名和密码");
    return;
  }

  try {
    const response = await loginService(form.email, form.password);

    // 确保后端返回的响应结构是你期望的
    if (response.code === 0) {
      ElMessage.success("登录成功");
      router.push("/guanli"); // 跳转到管理页面
    } else {
      ElMessage.error("用户名或密码错误");
    }
  } catch (error) {
    console.error("请求出错:", error);
    ElMessage.error("用户名或密码错误");
  }
};
</script>

<style scoped>
#divbox {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  background-image: url("@/assets/imgs/beijing.jpg");
  background-size: cover;
  background-position: center;
}

.login-form {
  width: 400px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

#logo {
  text-align: center;
  margin-bottom: 20px;
}

.login-title {
  font-family: "Microsoft YaHei";
  font-weight: bold;
  font-size: 24px;
  color: #4f5155;
}

img {
  border-radius: 50%;
  width: 100px;
  margin-bottom: 20px;
}

.register {
  color: #006a5a;
  cursor: pointer;
  text-decoration: underline;
  display: block;
  text-align: center;
}
</style>
